/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.tivo.hme.bananas;

import com.tivo.hme.interfaces.ILogger;
import com.tivo.hme.sdk.Resolution;
import com.tivo.hme.sdk.Resource;
import java.awt.Rectangle;
import java.lang.reflect.Field;

public class BScreenPlus extends BScreen implements PlusView {
    private boolean disposeResourceOnRemove;
    protected Object titleColor;
    protected Object titleShadowColor;
    protected Integer titleShadowOffset;
    protected Object titleFont;
    protected Object paragraphColor;
    protected Object paragraphFont;

	public BScreenPlus(BApplicationPlus app, Resolution theResolution) {
		super(app, theResolution);
        init();
	}

	public BScreenPlus(BApplicationPlus app) {
		super(app);
        init();
	}
    
    private void init() {
        //override the focus manager
        try {
            Field field = BScreen.class.getDeclaredField("focusMgr");
            field.setAccessible(true);
            field.set(this, createFocusManager());
        } catch (Exception e) {
            getApp().getLogger().log(ILogger.LOG_WARNING, 
                    "Cannot override focus manager for " + this);
        }
    }
    
    protected BFocusMgrPlus createFocusManager() {
        return new BFocusMgrPlus(getBApp());
    }
	
	public BApplicationPlus getBApp() {
		return (BApplicationPlus)super.getBApp();
	}

    @Override
    public void remove(Resource animation) {
        PlusSupport.viewRemoveNotify(this);
        super.remove(animation);
    }
    
    protected void viewRemoveNotify() {
        if (getDisposeResourceOnRemove()) {
            Resource resource = getResource();
            if (resource != null) {
                //remove the resource
                resource.remove();
            }
        }
    }

    public boolean getDisposeResourceOnRemove() {
        return disposeResourceOnRemove;
    }

    public void setDisposeResourceOnRemove(boolean disposeResourceOnRemove) {
        this.disposeResourceOnRemove = disposeResourceOnRemove;
    }
    
    public Rectangle getScreenFocusBounds() {
        BPoint s = toScreen();
        return new Rectangle(s.x, s.y, getWidth(), getHeight());
    }
    
    public Resource getDefaultTitleFont() {
        BSkin skin = getBApp().getSkin();
        BSkin.Element e = skin.get(IBananasPlus.H_TITLE_FONT);
        if (e != null) {
            return e.getResource();
        } else {
            return null;
        }
    }
    
    public Resource getDefaultTitleColor() {
        BSkin skin = getBApp().getSkin();
        BSkin.Element e = skin.get(IBananasPlus.H_TITLE_TEXT_COLOR);
        if (e != null) {
            return e.getResource();
        } else {
            return null;
        }
    }
    
    public Resource getDefaultTitleShadowColor() {
        BSkin skin = getBApp().getSkin();
        BSkin.Element e = skin.get(IBananasPlus.H_TITLE_SHADOW_COLOR);
        if (e != null) {
            return e.getResource();
        } else {
            return null;
        }
    }
    
    public int getDefaultTitleShadowOffset() {
        BSkin skin = getBApp().getSkin();
        BSkin.Element e = skin.get(IBananasPlus.H_TITLE_SHADOW_COLOR);
        if (e != null) {
            return e.getInt(IBananasPlus.P_OFFSET, 3);
        } else {
            return 3;
        }
    }
    
    public Resource getDefaultParagraphColor() {
        BSkin skin = getBApp().getSkin();
        BSkin.Element e = skin.get(IBananasPlus.H_PARAGRAPH_TEXT_COLOR);
        if (e != null) {
            return e.getResource();
        } else {
            return null;
        }
    }
    
    public Resource getDefaultParagraphFont() {
        BSkin skin = getBApp().getSkin();
        BSkin.Element e = skin.get(IBananasPlus.H_PARAGRAPH_FONT);
        if (e != null) {
            return e.getResource();
        } else {
            return null;
        }
    }

    public Object getTitleColor() {
        if (titleColor == null) {
            titleColor = getDefaultTitleColor();
        }
        return titleColor;
    }

    public void setTitleColor(Object titleColor) {
        this.titleColor = titleColor;
    }

    public Object getTitleFont() {
        if (titleFont == null) {
            titleFont = getDefaultTitleFont();
        }
        return titleFont;
    }

    public void setTitleFont(Object titleFont) {
        this.titleFont = titleFont;
    }

    public Object getTitleShadowColor() {
        if (titleShadowColor == null) {
            titleShadowColor = getDefaultTitleShadowColor();
        }
        return titleShadowColor;
    }

    public void setTitleShadowColor(Object titleShadowColor) {
        this.titleShadowColor = titleShadowColor;
    }

    public int getTitleShadowOffset() {
        if (titleShadowOffset == null) {
            titleShadowOffset = getDefaultTitleShadowOffset();
        }
        return titleShadowOffset;
    }

    public void setTitleShadowOffset(int titleShadowOffset) {
        this.titleShadowOffset = titleShadowOffset;
    }

    public Object getParagraphColor() {
        if (paragraphColor == null) {
            paragraphColor = getDefaultParagraphColor();
        }
        return paragraphColor;
    }

    public void setParagraphColor(Object paragraphColor) {
        this.paragraphColor = paragraphColor;
    }

    public Object getParagraphFont() {
        if (paragraphFont == null) {
            paragraphFont = getDefaultParagraphFont();
        }
        return paragraphFont;
    }

    public void setParagraphFont(Object paragraphFont) {
        this.paragraphFont = paragraphFont;
    }
}
